%creates 5 column matrix of force data 

%Input condition matrices (heat, walks, weights)
%1: three used actions
%2: used action in location 1
%3: used action in location 2
%4: used action in location 3
%5: unused action
%6: actual action
%7: alternative action 1
%8: alternative action 2
%9: condition

%Output numeric matrix (fulldata) column IDs:
%1: sub
%2: duration
%3: captcha score
%4: scenario
%5: condition (norm alt vs. abnorm alt)
%6: alt 1 norm (this is the critical alternative (either a normal or
%abnormal action)
%7: alt 2 norm (this is the non-critical alternative (always an abnormal action)
%8: alt 1 value 
%9: alt 2 value
%10: comp1 correct/not correct
%11: comp2 correct/not correct


%Output cell matrix column IDs:
%1: sub
%2: scenario
%3: actual action
%4: alt 1 (crit alternative)
%5: alt 2 (non-critical alternative)
%6: response to comp check 3

clear all

%specify raw data file here
load('exp5a_rawdata.mat')
load('exp5a_condition_matrices.mat')
raw_data=exp5arawdata;
temp=raw_data;
temp(:,1:2)=[];


%identify any subjects who gave no resonse
[rows,cols]=size(temp);
delete=[];
for i=1:rows
    notempty=not(cellfun('isempty',temp(i,:)));
    if sum(notempty)~=8
        delete=[delete,i];
    end
end

%create an index of the blank rows 
ctr=1;
delete_idx=[];
for j=1:rows
    if j==delete(ctr);
        delete_idx(j)=0;
        if ctr<length(delete)
        ctr=ctr+1;
        end
    else delete_idx(j)=1;
    end
end
delete_idx=delete_idx';


%create a new dataset eliminating those subjects
full_clean_data=raw_data;
full_clean_data=full_clean_data(delete_idx==1,:);

[rows,cols]=size(full_clean_data);
clean_data=full_clean_data(:,3:cols); %this is the clean data excluding the duration and captcha score. This matches the number of columns in var_names below. 

%remove first two cols of var_names because they are NaN (must add 2 to any
%index derived from var_names when referring to full_clean_data)
var_names=var_names(:,3:length(var_names));

%%
%Collect the data
fulldata=[];
fullcelldata={};
[rows,cols]=size(clean_data);

for cursub=1:rows
    %1. add subnum to full data
    fulldata(cursub,1)=cursub;
    fullcelldata{cursub,1}=cursub;
    %2. add duration to full data
    fulldata(cursub,2)=full_clean_data{cursub,1};
    %3. add captcha score to full data
    fulldata(cursub,3)=full_clean_data{cursub,2};
    
    %for this subject, get the columns where there is data
    cond_index=not(cellfun('isempty',clean_data(cursub,:)));
    sub_conds=var_names(:,cond_index);
    sub_data=clean_data(cursub,cond_index);
    
    %find the scneario
    scen_array={'heat','walks','weights'};
    for c=1:length(scen_array)
        cur_scen=strfind(sub_conds(1,1),scen_array{c});
        if not(cellfun('isempty',cur_scen));
            fulldata(cursub,4)=c; %4. set fulldata column 4 to scenario number
            fullcelldata{cursub,2}=scen_array{c};
            scenario=scen_array{c};
            scen_num=c;
        end
    end
    
    %%
    %Set the critical information for each scenario
    %identify the right, scenario specific condition matrix and specify
    %keywords to identify different actions
    if scen_num==1
        cond_mat=heat;
        norm1='shorts';
        norm2='tank';
        abnorm1='speedo';
        abnorm2='underwear';

    elseif scen_num==2
        cond_mat=walks;
        norm1='park';
        norm2='neighborhood';
        abnorm1='house';
        abnorm2='backyard';
        
    elseif scen_num==3
        cond_mat=weights;
        norm1='dumbbells';
        norm2='machine';
        abnorm1='dog';
        abnorm2='water';
    end
    
    %Identify the right row in the condition matrix
    [r,c]=size(cond_mat);
    for currow=1:r
        rowcheck=strfind(sub_conds(1,1),[num2str(currow),'_',scenario]);%find the first number that comes before the scenario name in the variable list
        if not(cellfun('isempty',rowcheck)); %if there is a number there
            rownum=currow; %set this as the row number in the condition matrix for this subject.
        end
    end

    %5. add condition to full data
    condcheck=strfind(cond_mat{rownum,1},'normalt');
        if not(isempty(condcheck))
            fulldata(cursub,5)=1;
        else fulldata(cursub,5)=2;
        end

   
    %%
    %6. Get the actual and alternative actions 
    if fulldata(cursub,5)==1 %if it is a normal alternative condition
        act_check=strfind(cond_mat(rownum,6),norm1); %look in the actual action column for one of the specific normal actions
        if not(cellfun('isempty',act_check)) %if it is there
            act_action=norm1; %set the actual action to this action
            alt1=norm2; %set the critical alternative (alt1) to the other normal action
        else
            act_action=norm2; %set the actual action to the other action 
            alt1=norm1; %set the critical alternative (alt1) to the other normal action
        end
        
        alt2_check=strfind(cond_mat(rownum,8),abnorm1); %look in the non-critical alternative column for one of the abnormal actions
        if not(cellfun('isempty', alt2_check)) %if it is there
            alt2=abnorm1; %set alt2 (the non-critical alternative) to this abnormal action
        else
            alt2=abnorm2; %otherwise set alt2 to the other abnormal action
        end
    
    elseif fulldata(cursub,5)==2 %if it is an abnormal alternative condition
        act_check=strfind(cond_mat(rownum,6),norm1); %look in the actual action column for one of the specific normal actions
        if not(cellfun('isempty',act_check)) %if it is there
            act_action=norm1; %set the actual action to this action
        else 
            act_action=norm2; %otherwise, set it to the other actual action
        end
        
        alt1_check=strfind(cond_mat(rownum,7),abnorm1); %look in the critical alternative column for one of the abnormal actions
        if not(cellfun('isempty', alt1_check)) %if it is there
            alt1=abnorm1; %set alt1 (the critical alternative) to this action
            alt2=abnorm2; %set alt2 to the aother abnormal aciton
        else
            alt1=abnorm2; %if it is not there, do the reverse
            alt2=abnorm1;
        end
    end

    %Put all of the actual and alternative information into fullcelldata
    fullcelldata{cursub,3}=act_action;
    fullcelldata{cursub,4}=alt1;
    fullcelldata{cursub,5}=alt2;

    
    %%
    %Get responses
    %find the col that asks the force question
    force_check=strfind(sub_conds(1,:),'force');
    force_index=not(cellfun('isempty',force_check));
    
    %find the cols that ask the unusual question
    norm_check=strfind(sub_conds(2,:),'unusual');
    norm_index=not(cellfun('isempty',norm_check)); 
    
    %find the cols that ask the value question
    val_check=strfind(sub_conds(2,:),'good idea');
    val_index=not(cellfun('isempty',val_check));
    
    %find the cols that ask the about the critical alternative
    alt1_check=strfind(sub_conds(2,:),'Field-7');
    alt1_index=not(cellfun('isempty',alt1_check));
    
    %find the cols that ask the about the other alternative
    alt2_check=strfind(sub_conds(2,:),'Field-8');
    alt2_index=not(cellfun('isempty',alt2_check));
    
    alt1_normindex=and(norm_index,alt1_index);
    alt2_normindex=and(norm_index,alt2_index);
    
    alt1_valindex=and(val_index,alt1_index);
    alt2_valindex=and(val_index,alt2_index);
    
    
    %find the norm column that asks about the critical alternative item
    fulldata(cursub,6)=str2num(sub_data{1,alt1_normindex}); %save it to fulldata
    
    %find the norm colum that asks about the other alternative item
    fulldata(cursub,7)=str2num(sub_data{1,alt2_normindex}); %save it to fulldata
    
    %find the norm column that asks about the critical alternative item
    fulldata(cursub,8)=str2num(sub_data{1,alt1_valindex}); %save it to fulldata
    
    %find the norm colum that asks about the other alternative item
    fulldata(cursub,9)=str2num(sub_data{1,alt2_valindex}); %save it to fulldata
    
    %find the force column
    fulldata(cursub,10)=str2num(sub_data{1,force_index}); %save it to fulldata
    
    %%
    %Add comprehension check answers to results
    %check for correct answer to comp check question one (should be field
    %5)
    comp1_check=strfind(sub_data(6),'5');
    if not(cellfun('isempty',comp1_check))
        fulldata(cursub,11)=1;
    else fulldata(cursub,11)=0;
    end
    
    %check for correct answer to comp check question two (should be field 6)
    comp2_check=strfind(sub_data(7),'6');
    if not(cellfun('isempty',comp2_check))
        fulldata(cursub,12)=1;
    else fulldata(cursub,12)=0;
    end
    
    %add open ended comp check question 3 to full cell data
    fullcelldata(cursub,6)=sub_data(8);
end

%Comprehension checks

%remove subjects who failed the comprehension check question 1
finalfulldata=fulldata(fulldata(:,11)==1,:);
finalfullcelldata=fullcelldata(fulldata(:,11)==1,:);

%remove subjects who failed the comprehension check question 2
finalfullcelldata=finalfullcelldata(finalfulldata(:,12)==1,:);
finalfulldata=finalfulldata(finalfulldata(:,12)==1,:);


save finaldata_precomp.mat finalfulldata finalfullcelldata



%%
%%%%%%%%%
%%%%%%%%% NOTE: Sometimes writetable leaves out rows! DOUBLE CHECK%%%%%%%%%
%%%%%%%%%

%At this point, write fullcelldata to csv, check the open ended comp check
%questiosn 3 and manually create a column 7 that is a 1 for correct and a 0
%for incorrect. 
finalfullcelldata_table=cell2table(finalfullcelldata);

if ~exist('exp5a_fullcelldata_comp3.csv')
    writetable(finalfullcelldata_table,'exp5a_fullcelldata_comp3.csv')
end


%Manually edit full cell data, save it as study5e_fullcelldata_comp3.csv
%and then manually import it and save it as fullcelldata_withcomp.mat
if exist('fullcelldata_withcomp.mat')
    load('fullcelldata_withcomp.mat')
    
    comp3=cell2mat(fullcelldata_withcomp(:,7));
    finalfulldata=finalfulldata(comp3==1,:);
    finalfullcelldata=fullcelldata_withcomp(comp3==1,:);
    
    csvwrite('exp5a_fulldata_foranalysis.csv',finalfulldata);

    finalfullcelldata_table=cell2table(finalfullcelldata);
    writetable(finalfullcelldata_table,'exp5a_fullcelldata_foranalysis.csv');

    [cellrows,cellcols]=size(finalfullcelldata);
    alldata=[num2cell(finalfulldata),finalfullcelldata];
    alldata_table=cell2table(alldata);
    writetable(alldata_table,'exp5a_completefinaldata_foranalysis.csv');

end
